import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  Query,
  Put,
} from '@nestjs/common';
import { RestaurantService } from './restaurant.service';
import {
  CreateRestaurantDto,
  UpdateRestaurantDto,
  QueryRestaurantDto,
} from './dto';
import { ApiResponse } from '../../common/interfaces/api-response.interface';
import { Restaurant } from '../../entities/restaurant.entity';

@Controller('restaurants')
export class RestaurantController {
  constructor(private readonly restaurantService: RestaurantService) {}

  // 获取餐厅列表（分页）
  @Get()
  async getRestaurants(@Query() query: QueryRestaurantDto) {
    const data = await this.restaurantService.getRestaurants(query);
    return {
      success: true,
      data,
    } as ApiResponse<typeof data>;
  }

  // 获取所有餐厅
  @Get('all')
  async getAllRestaurants() {
    const data = await this.restaurantService.getAllRestaurants();
    return {
      success: true,
      data,
    } as ApiResponse<Restaurant[]>;
  }

  // 获取餐厅分类列表
  @Get('categories')
  async getCategories() {
    const data = await this.restaurantService.getCategories();
    return {
      success: true,
      data,
    } as ApiResponse<string[]>;
  }

  // 获取频率规则列表
  @Get('frequency-rules')
  getFrequencyRules() {
    const data = this.restaurantService.getFrequencyRules();
    return {
      success: true,
      data,
    } as ApiResponse<string[]>;
  }

  // 搜索餐厅
  @Get('search')
  async searchRestaurants(@Query('keyword') keyword: string) {
    const data = await this.restaurantService.searchRestaurants(keyword);
    return {
      success: true,
      data,
    } as ApiResponse<Restaurant[]>;
  }

  // 根据ID获取餐厅
  @Get(':id')
  async getRestaurantById(@Param('id') id: string) {
    const data = await this.restaurantService.getRestaurantById(id);
    return {
      success: true,
      data,
    } as ApiResponse<Restaurant>;
  }

  // 创建餐厅
  @Post()
  async createRestaurant(@Body() createRestaurantDto: CreateRestaurantDto) {
    const data =
      await this.restaurantService.createRestaurant(createRestaurantDto);
    return {
      success: true,
      data,
      message: '餐厅创建成功',
    } as ApiResponse<Restaurant>;
  }

  // 更新餐厅
  @Put(':id')
  async updateRestaurant(
    @Param('id') id: string,
    @Body() updateRestaurantDto: UpdateRestaurantDto,
  ) {
    const data = await this.restaurantService.updateRestaurant(
      id,
      updateRestaurantDto,
    );
    return {
      success: true,
      data,
      message: '餐厅更新成功',
    } as ApiResponse<Restaurant>;
  }

  // 删除餐厅
  @Delete(':id')
  async deleteRestaurant(@Param('id') id: string) {
    await this.restaurantService.deleteRestaurant(id);
    return {
      success: true,
      data: null,
      message: '餐厅删除成功',
    } as ApiResponse<null>;
  }

  // 批量删除餐厅
  @Delete('batch/delete')
  async batchDeleteRestaurants(@Body('ids') ids: string[]) {
    await this.restaurantService.batchDeleteRestaurants(ids);
    return {
      success: true,
      data: null,
      message: '餐厅批量删除成功',
    } as ApiResponse<null>;
  }
}
